<template>
  <div class="panel-content">
    <div class="panel-header">
      <div class="panel-title">
        <span class="panel-icon">📊</span>
        <span>事件时间线</span>
      </div>
      <el-button size="small" type="primary" @click="addEvent">
        <el-icon><Plus /></el-icon>
        新增
      </el-button>
    </div>

    <div class="events-timeline" v-loading="loading" element-loading-text="正在加载事件线...">
      <el-card
        v-for="event in events"
        :key="event.id"
        class="event-item"
        shadow="hover"
      >
        <div class="event-marker"></div>
        <div class="event-content">
          <div class="event-header">
            <h4 @click="editEvent(event)">{{ event.title }}</h4>
            <div class="event-actions">
              <el-dropdown @command="(cmd) => handleEventAction(cmd, event)" trigger="click">
                <el-button size="small" type="text" @click.stop>
                  <el-icon><MoreFilled /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="edit">
                      <el-icon><Edit /></el-icon>
                      编辑
                    </el-dropdown-item>
                    <el-dropdown-item command="delete" divided>
                      <el-icon><Delete /></el-icon>
                      删除
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
          <el-tooltip
            :content="event.description"
            placement="right"
            :disabled="event.description.length <= 80"
            effect="light"
            :show-after="300"
          >
            <p class="event-desc event-desc-truncated">
              {{ event.description.length > 80 ? event.description.substring(0, 80) + '...' : event.description }}
            </p>
          </el-tooltip>
          <div class="event-meta">
            <el-tag size="small">{{ event.chapter }}</el-tag>
            <span class="event-time">{{ event.time }}</span>
          </div>
        </div>
      </el-card>

      <el-empty v-if="events.length === 0" description="暂无事件记录">
        <el-button size="small" @click="addEvent">添加第一个事件</el-button>
      </el-empty>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'
import { Plus, MoreFilled, Edit, Delete } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  events: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits([
  'add-event',
  'edit-event',
  'event-action'
])

// 方法
const addEvent = () => {
  emit('add-event')
}

const editEvent = (event) => {
  emit('edit-event', event)
}

const handleEventAction = (command, event) => {
  emit('event-action', command, event)
}
</script>

<style scoped>
.panel-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  overflow: hidden;
  height: calc(100vh - 150px);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e4e7ed;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  
}

.panel-icon {
  font-size: 18px;
}

.events-timeline {
  flex: 1;
  overflow-y: auto;
  padding-right: 4px;
  max-height: calc(100vh - 190px);
}

.event-item {
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid #e4e7ed;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.event-item :deep(.el-card__body) {
  padding: 12px;
  display: flex;
  align-items: flex-start;
}

.event-item:hover {
  border-color: #409eff;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
  transform: translateY(-1px);
}

.event-item:hover :deep(.el-card__body) {
  background-color: #f0f9ff;
}

.event-item:hover .event-actions {
  opacity: 1;
}

.event-marker {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #409eff;
  margin-top: 6px;
  margin-right: 10px;
  flex-shrink: 0;
}

.event-content {
  flex: 1;
}

.event-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.event-content h4 {
  margin: 0;
  font-size: 14px;
  
  cursor: pointer;
}

.event-content p {
  margin: 0 0 4px 0;
  font-size: 13px;
  color: #606266;
  line-height: 1.4;
}

.event-desc-truncated {
  cursor: help;
  transition: color 0.2s ease;
}

.event-desc-truncated:hover {
  
}

.event-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #909399;
  margin-top: 6px;
}

.event-time {
  margin-left: 8px;
  
}

.event-actions {
  opacity: 0;
  transition: opacity 0.3s;
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
}
</style>
